//
//  WYStatusPicView.swift
//  WYWeibo
//
//  Created by wangyi on 16/7/16.
//  Copyright © 2016年 wangyi. All rights reserved.
//

import UIKit
import SDWebImage
//定义标示符
private let PictureViewCellIdentifier = "PictureViewCellIdentifier"

//间距
private let itemMargin: CGFloat = 5
//宽度
private let itemWidth: CGFloat = (ScreenWidth - 2 * HomeCellMargin - 2 * itemMargin ) / 3

class WYStatusPicView: UICollectionView {
    

    
    var picUrls: [WYStatusPictureInfos]? {
    
        didSet {
            //设置配图文字
            messageLabel.text = "\(picUrls?.count ?? 0)"
            let size = calcPictureViewSize(picUrls?.count ?? 0)
            //配图文字大小
            self.snp_updateConstraints { (make) -> Void in
                make.size.equalTo(size)
            }
            //重加载数据
            self.reloadData()
        }
    }
    // MARK: -- 懒加载
    private lazy var messageLabel: UILabel = {
        
       let label = UILabel(textColor: UIColor.yellowColor(), fontSize: 30)
        return label
        
    }()
    
    //重写
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
       
        let flowLayout = UICollectionViewFlowLayout()
        
        //设置大小
        flowLayout.itemSize = CGSize(width: itemWidth, height: itemWidth)
        flowLayout.minimumInteritemSpacing = itemMargin
        flowLayout.minimumLineSpacing = itemMargin
        super.init(frame: frame, collectionViewLayout: flowLayout)
        
       setUPUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setUPUI() {
        
        registerClass(WYStatusPictureVieCell.self, forCellWithReuseIdentifier: PictureViewCellIdentifier)
        dataSource = self
        
        addSubview(messageLabel)
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
    }
    
    /*!
    
     //根据图片个数计算配图大小
    - parameter count: 配图葛素
    
    - returns: 配图大小
    */
    private func calcPictureViewSize(count: Int) -> CGSize {
       
        //列数
        let cols = count > 3 ? 3 : count
        //行数
        let rows = (count - 1) / 3 + 1
        //计算当前配图大小
        let width = CGFloat(cols) * itemWidth + CGFloat(cols - 1) * itemMargin
        let height = CGFloat(rows) * itemWidth + CGFloat(rows - 1) * itemMargin
        let size = CGSize(width: width, height: height)
        
        return size
        
    }
    


    
}

    // MARK: UICollectionView 的数据源方法

extension WYStatusPicView: UICollectionViewDataSource {
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return picUrls?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(PictureViewCellIdentifier, forIndexPath: indexPath) as! WYStatusPictureVieCell
      
        cell.picInfo = picUrls![indexPath.item]
        
        return cell
    }
    
}

class WYStatusPictureVieCell: UICollectionViewCell {
    
    var picInfo: WYStatusPictureInfos? {
    
        didSet {
            if let imageUrl = picInfo?.thumbnail_pic {
            
                 imageView.sd_setImageWithURL(NSURL(string: imageUrl), placeholderImage: UIImage(named: "timeline_image_placeholder"))
                
                if imageUrl.hasSuffix(".gif") {
                
                    gifImageView.hidden = false
                } else {
                
                    gifImageView.hidden = true
                }
                gifImageView.hidden = !imageUrl.hasSuffix(".gif")
            }
            
        }
    }
    // MARK:  -- 
    private lazy var imageView: UIImageView = {
    
        let view = UIImageView(image: UIImage(named: "timeline_image_placeholder"))
       
        //保证原始图片 比例显示 图片会超出 
        view.contentMode = UIViewContentMode.ScaleAspectFill
        //剪切超出部分图片
       view.clipsToBounds = true
        return view
    }()
    
    //gif 图片
    private lazy var gifImageView: UIImageView = UIImageView(image: UIImage(named: "timeline_image_gif"))
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setUpUI() {
        //添加控件
        contentView.addSubview(imageView)
        contentView.addSubview(gifImageView)
        //设置约束
        imageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
        gifImageView.snp_makeConstraints { (make) -> Void in
            make.trailing.equalTo(imageView)
            make.bottom.equalTo(imageView)
        }
    }
    
    
}